package com.gudong.performance.service;

import com.gudong.performance.dao.entity.PerformanceInfo;
import com.gudong.performance.dao.entity.PerformanceInfoExample;
import com.gudong.performance.dao.mapper.PerformanceInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类功能描述：
 *
 * @author：刘富国
 * @createTime：2019-04-02 10:02
 */
@Service
public class PerformanceServiceImpl implements PerformanceService {

    @Autowired
    private PerformanceInfoMapper performanceInfoMapper;

    @Override
    public List<PerformanceInfo> getPerformanceList(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        //开始时间
        Date start = calendar.getTime();
        calendar.add(Calendar.MONTH, 1);
        //结束时间
        Date end = calendar.getTime();

        PerformanceInfoExample example = new PerformanceInfoExample();
        PerformanceInfoExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("1");
        criteria.andPerformanceDateGreaterThanOrEqualTo(start);
        criteria.andPerformanceDateLessThanOrEqualTo(end);
        example.setOrderByClause("create_time desc");
        return performanceInfoMapper.selectByExample(example);
    }

    @Override
    public void editPerformance(PerformanceInfo performanceInfo) {
        PerformanceInfo info = performanceInfoMapper.selectByPrimaryKey(performanceInfo.getId());
        if (!StringUtils.isEmpty(performanceInfo.getNumber())) {
            info.setNumber(performanceInfo.getNumber());
        }
        if (!StringUtils.isEmpty(performanceInfo.getPerformanceDate())) {
            info.setPerformanceDate(performanceInfo.getPerformanceDate());
        }
        if (!StringUtils.isEmpty(performanceInfo.getAmount())) {
            info.setAmount(performanceInfo.getAmount());
        }
        if (!StringUtils.isEmpty(performanceInfo.getCompanyName())) {
            info.setCompanyName(performanceInfo.getCompanyName());
        }
        if (!StringUtils.isEmpty(performanceInfo.getStatus())) {
            info.setStatus(performanceInfo.getStatus());
        }
        if (!StringUtils.isEmpty(performanceInfo.getUserName())) {
            info.setUserName(performanceInfo.getUserName());
        }
        performanceInfoMapper.updateByPrimaryKey(info);
    }

    @Override
    public Integer addPerformance(PerformanceInfo performanceInfo) {
        int id = performanceInfoMapper.insert(performanceInfo);
        return id;
    }

    @Override
    public void delPerformance(List<Integer> idList) {
        for (Integer id : idList) {
            PerformanceInfo info = performanceInfoMapper.selectByPrimaryKey(id);
            if (info != null) {
                info.setStatus("2");
                performanceInfoMapper.updateByPrimaryKey(info);
            }
        }

    }

    @Override
    public List<String> getNameList(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        //开始时间
        Date start = calendar.getTime();
        calendar.add(Calendar.MONTH, 1);
        //结束时间
        Date end = calendar.getTime();

        PerformanceInfoExample example = new PerformanceInfoExample();
        PerformanceInfoExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("1");
        criteria.andPerformanceDateGreaterThanOrEqualTo(start);
        criteria.andPerformanceDateLessThanOrEqualTo(end);
        example.setDistinct(true);
        example.getOredCriteria();
        List<PerformanceInfo> infoList = performanceInfoMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(infoList)) {
            return null;
        }
        List<String> nameList = infoList.stream().map(o -> o.getUserName()).distinct().collect(Collectors.toList());
        return nameList;
    }

    @Override
    public List<PerformanceInfo> getListByName(String name, Date time) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        //开始时间
        Date start = calendar.getTime();
        calendar.add(Calendar.MONTH, 1);
        //结束时间
        Date end = calendar.getTime();

        PerformanceInfoExample example = new PerformanceInfoExample();
        PerformanceInfoExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("1");
        criteria.andUserNameEqualTo(name);
        criteria.andPerformanceDateGreaterThanOrEqualTo(start);
        criteria.andPerformanceDateLessThanOrEqualTo(end);
        return performanceInfoMapper.selectByExample(example);
    }
}
